﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vmago.Models
{
    public class RepositoryShopProduct:IShopProduct 
    {
        #region IShopProduct Members

        private ShopProductDataContext ds;

        public RepositoryShopProduct()
        {
            ds = new ShopProductDataContext();
        }

        public IEnumerable<ShopProduct> getShopProduct(int shopID)
        {
            IEnumerable<ShopProduct> c = null;
            try
            {
                c = from p in ds.ShopProducts
                    where p.shopID.Equals(shopID)
                    select p;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public IEnumerable<ShopProductImage> getShopProductImage(int shopID, int productID)
        {
            IEnumerable<ShopProductImage> c = null;
            try
            {
                c = from p in ds.ShopProductImages
                    where p.shopID.Equals(shopID) && p.productID.Equals(productID)
                    select p;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }
        //用于查询商户全部图片用的接口
        public IEnumerable<ShopProductImage> getShopProductImage(int shopID)
        {
            IEnumerable<ShopProductImage> c = null;
            try
            {
                c = from p in ds.ShopProductImages
                    where p.shopID.Equals(shopID)
                    select p;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }


        #endregion

        #region IShopProduct Members


        public int createShopProduct(int shopID,int productType, string productName,string  description, decimal price,int grade, int language)
        {
            int c = 0;
            try
            {
                ds.updateShopProduct("INS", shopID, productType, productName, description, price, grade, language, 0);
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public int updateShopProduct(int shopID, int productType, string productName, string description, decimal price, int grade, int language, int productID)
        {
            int c = 0;
            try
            {
                ds.updateShopProduct("UPD", shopID, productType, productName, description, price, grade, language, productID);
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public int deleteShopProduct(int productID)
        {
            int c = 0;
            try
            {
                ds.updateShopProduct("DEL", 0, 0, "", "", 0, 0, 0, productID);
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public int createShopProductImage( int shopID,int productID,int imageType,string fileName,int type, string description)
        {
            int c = 0;
            try
            {
                ds.updateShopProductImage("INS",0,shopID,imageType,productID,fileName,description);
                
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public int updateShopProductImage(int imageID,int shopID, int productID, int imageType, string fileName, int type, string description)
        {
            int c = 0;
            try
            {

                ds.updateShopProductImage("UPD", imageID, shopID, imageType, productID, fileName, description);
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public int deleteShopProductImage(int imageID)
        {
            int c = 0;
            try
            {
                ds.updateShopProductImage("UPD", imageID, 0, 0, 0, "", "");
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        #endregion
    }
}
